package com.Lyoto.util.weixin.utils;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Lyoto
 * @Description
 * @Date 2022-09-20 10:20
 * @Version
 **/
public class YmlUtil {
    public static Map yaml;
    static {
    yaml = loadYml("weixin.yml");
    }
    public static final String SPLIT_CHAR = ".";

    public static String getByTag(String ... names){
        if (names.length == 1){
            return prase(names[0]);
        }
        StringBuilder builder = new StringBuilder();
        for (String s : names){
            builder.append(prase(s));
        }
        return builder.toString();
    }
    public static String prase(String name){
        String re = null;
        if (name.contains(SPLIT_CHAR)) {
            String[] split = name.split("\\"+SPLIT_CHAR);
            LinkedHashMap<String,Object> map = (LinkedHashMap<String,Object>) yaml.get(split[0]);
            for (int i = 0; i < split.length; i++) {
                if (i == split.length -1 && map.get(split[i]) instanceof String){
                   re = (String) map.get(split[i]);
                   break;
                }
                if (i != 0){
                    map = (LinkedHashMap<String,Object>) map.get(split[i]);
                }
            }
        return re;
        }
        return (String) yaml.get(name);
    }
    private static LinkedHashMap<String,Object> loadYml(String configName){
        Yaml yaml = new Yaml();
        InputStream stream = YmlUtil.class.getClassLoader().getResourceAsStream(configName);
        return yaml.load(stream);
    }
    
    public static void main(String[] args) {
        String prase = YmlUtil.getByTag("weixin.api","weixin.message.path.send");
        System.out.println(prase);
    }

}
